package com.sky.demo.global;

import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.view.inputmethod.InputMethodManager;

import com.sky.demo.utils.BaseInterface;
import com.sky.demo.utils.ToastUtils;
/**
 * @ClassName: IntroductoryActivity
 * @Description: TODO 基础activity
 * @author LiBin
 * @date 2015年3月26日 下午4:01:00
 */
public class BaseActivity extends AppCompatActivity implements BaseInterface {
    public Context mContext = null;
    public MyApplication myApplication;
    public ProgressDialog pg = null;
    public NotificationManager notificationManager;

    public void showToast(String text) {
        ToastUtils.showShort(this, text);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        pg = new ProgressDialog(mContext);
        myApplication = (MyApplication) getApplicationContext();
        myApplication.getActivityManager().pushActivity(this);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        myApplication.getActivityManager().popActivity(this);
    }

    /* (non-Javadoc)
     * 监听activity加载完毕之后开始执行
     * @see android.app.Activity#onWindowFocusChanged(boolean)
     */
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);

    }

    /**
     * 初始化页面布局
     */
//	abstract void iniView();
    @Override
    public ProgressDialog getProgressDialog() {
        return pg;
    }

    /**
     * 在这里开启所有需要开启的服务
     */
    @Override
    public void startService() {

    }

    /**
     * 在这里关闭所有需要开启的服务
     */
    @Override
    public void stopService() {

    }

    /**
     * 停止服务并结束所有的Activity退出应用
     */
    @Override
    public void isExit() {
        new AlertDialog.Builder(mContext).setTitle("确定退出吗?")
                .setNeutralButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        stopService();
                        myApplication.exit();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                }).show();
    }

    /**
     * 判断是否有网络连接,没有返回false
     */
    @Override
    public boolean hasInternetConnected() {
        ConnectivityManager manager = (ConnectivityManager) mContext
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (manager != null) {
            NetworkInfo network = manager.getActiveNetworkInfo();
            if (network != null && network.isConnectedOrConnecting()) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是否有网络连接，若没有，则弹出网络设置对话框，返回false
     */
    @Override
    public boolean validateInternet() {
        ConnectivityManager manager = (ConnectivityManager) mContext
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (manager == null) {
            openWirelessSet();
            return false;
        } else {
            NetworkInfo[] info = manager.getAllNetworkInfo();
            if (info != null) {
                for (int i = 0; i < info.length; i++) {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        openWirelessSet();
        return false;
    }

    /**
     * 判断GPS定位服务是否开启
     */
    @Override
    public boolean hasLocationGPS() {
        LocationManager manager = (LocationManager) mContext
                .getSystemService(Context.LOCATION_SERVICE);
        if (manager
                .isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断基站定位是否开启
     */
    @Override
    public boolean hasLocationNetWork() {
        LocationManager manager = (LocationManager) mContext
                .getSystemService(Context.LOCATION_SERVICE);
        if (manager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 检查内存卡可读
     */
    @Override
    public void checkMemoryCard() {
        if (!Environment.MEDIA_MOUNTED.equals(Environment
                .getExternalStorageState())) {
            new AlertDialog.Builder(mContext)
                    .setTitle("检测内存卡")
                    .setMessage("请检查内存卡")
                    .setPositiveButton("设置",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog,
                                                    int which) {
                                    dialog.cancel();
                                    Intent intent = new Intent(
                                            Settings.ACTION_SETTINGS);
                                    mContext.startActivity(intent);
                                }
                            })
                    .setNegativeButton("退出",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog,
                                                    int which) {
                                    dialog.cancel();

                                }
                            }).create().show();
        }
    }

    /**
     * 打开网络设置对话框
     */
    public void openWirelessSet() {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);
        dialogBuilder
                .setTitle("网络设置")
                .setMessage("检查网络")
                .setPositiveButton("网络设置",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which) {
                                dialog.cancel();
                                Intent intent = new Intent(
                                        Settings.ACTION_WIRELESS_SETTINGS);
                                mContext.startActivity(intent);
                            }
                        })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int whichButton) {
                        dialog.cancel();
                    }
                });
        dialogBuilder.show();
    }

    /**
     * 关闭键盘
     */
    public void closeInput() {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputMethodManager != null && this.getCurrentFocus() != null) {
            inputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus()
                    .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    /**
     * 发出Notification
     *
     * @param iconId       图标
     * @param contentTitle 标题
     * @param contentText  你内容
     * @param activity
     */
    @SuppressWarnings("deprecation")
    public void PushNotification(int iconId, String contentTitle,
                                 String contentText, Class<?> activity, String to) {

        // 创建新的Intent，作为点击Notification留言条时， 会运行的Activity
        Intent notifyIntent = new Intent(this, activity);
        notifyIntent.putExtra("to", to);
        // 创建PendingIntent作为设置递延运行的Activity
        PendingIntent appIntent = PendingIntent.getActivity(mContext, 0,
                notifyIntent, 0);
        /* 创建Notication，并设置相关参数 */
        Notification myNoti = new Notification();
        // 点击自动消失
        myNoti.flags = Notification.FLAG_AUTO_CANCEL;
		/* 设置statusbar显示的icon */
        myNoti.icon = iconId;
		/* 设置statusbar显示的文字信息 */
        myNoti.tickerText = contentTitle;
		/* 设置notification发生时同时发出默认声音 */
        myNoti.defaults = Notification.DEFAULT_SOUND;
		/* 设置Notification留言条的参数 */
        myNoti.setLatestEventInfo(mContext, contentTitle, contentText,
                appIntent);
		/* 送出Notification */
        notificationManager.notify(0, myNoti);
    }

    /**
     * 返回上下文对象
     */
    @Override
    public Context getContext() {
        return mContext;
    }

    /**
     * 获取用户在线状态
     */
    @Override
    public boolean getUserOnlineState() {
        return false;
    }

    /**
     * 设置用户在线状态
     */
    @Override
    public void setUserOnlineState(boolean isOnline) {

    }
}
